#include <Resource.h>
#include <OpenCLTemplate.hpp>
#include <iostream>
using namespace std;

int main()
{
	//
	cout<<"Resource\n";

	Client client_obj; 		// Create Client object.
	OpenCLPlatforms obj;

	client_obj.CreateSocket(TCPSOCKET);	// Create Client Socket.
	client_obj.SetSocketOptions();

	client_obj.InitialiseAddress(DEFAULTCLIENTPORT);	// Without any argument default port will be chosen.
	client_obj.Bind();

	char server_name[24]="localhost";
	int server_port=5000;
	cout << "Enter Server name or IP: "; // Use localhost or 127.0.0.1 for local server.
	cin.getline(server_name, 24);
	//cout << "Server name or IP: "; // Use localhost or 127.0.0.1 for local server.
	//cout<< server_name<<endl;
	//cout << "Enter Server port: ";
	cout << "\nServer port: ";
	cout << server_port<<endl;
	//cin >> server_port;
	cout<<"Connected to server.."<<endl;
	// Connect to Server. Server name/IP and port are provided as arguments.
	client_obj.Connect(server_name, server_port);

	while(true)
	{


		/** recieving **/
		int *recived_buffer,*recived_bufferi,*recived_bufferj,*recived_size;
		int start_matrix_A, end_matrix_A;	
		client_obj.Receive();
		char* buff1	 =	client_obj.GetBuffer();
		recived_size =  (int*)buff1;
		char reply[]	= "Size received\n";
		int total_bytes	= recived_size[0];
		cout<<"size recieved by resource  "<<endl;
		client_obj.Send((void*)reply,sizeof(reply));
		recived_bufferj	= new int[total_bytes/4];
		recived_bufferi	= new int[total_bytes/4];
		int recived_bytes = 0;
		while(true)
		{
			if(client_obj.GetNumOfBytesReceived()==-1 || recived_bytes>=total_bytes)
				break;
			client_obj.Receive((void*)(recived_bufferi),(int)total_bytes);
			//		cout<<client_data->server_obj->GetNumOfBytesReceived(client_data->thread_number)<<endl;
			memcpy((char*)(recived_bufferj)+recived_bytes,(char*)recived_bufferi,client_obj.GetNumOfBytesReceived());
			recived_bytes += client_obj.GetNumOfBytesReceived();
		}
		cout << "No. of Bytes received: " <<recived_bytes<< endl;
		unsigned char keydata[] = "nucesfast";
		char *Decrypted = Encrypt(keydata, (char*)recived_bufferj, total_bytes);
		recived_buffer = (int*)Decrypted;

		obj.Initialize(recived_buffer);

		//	time_t start,end;
		//start=clock();

		obj.DetectPlatforms();
		obj.SetPlatformIDs();
		cout<<"break point 1"<<endl;

		obj.AllocateMemoryCPU();
		cout<<"break point 2"<<endl;
		//obj.MatrixGeneration();
		cout<<"break point 3"<<endl;
		obj.SetPlatformMatrices();
		cout<<"break point 4"<<endl;
		obj.CreateThreads();
		cout<<"break point 5"<<endl;
		obj.CombineMatrixC();

		cout<<"Start Sending result"<<endl;
		int	matrixC_length=obj.GetColumsMatrixB()* obj.GetRowsMatrixA();
		unsigned char *matrix_C_Buffer= new unsigned char[sizeof(int)*matrixC_length];
		memcpy(matrix_C_Buffer, obj.GetMatrixC(), sizeof(int)*matrixC_length);
		int totalbytes1=matrixC_length*4;
		int sentbytes=0;
		char *Encrypted = Encrypt(keydata, (char*)matrix_C_Buffer, totalbytes1);
		delete matrix_C_Buffer;
		while(true)
		{
			if(client_obj.GetNumOfBytesSent()==-1 || sentbytes>=totalbytes1)
				break;
			client_obj.Send((void*)(Encrypted+sentbytes),totalbytes1);//sending matrix 
			sentbytes+=client_obj.GetNumOfBytesSent();
		}
		//obj.MatrixStore(MATRIXC);

		cout<<"Sending done"<<endl;
		delete Encrypted;
		delete recived_bufferi;
		delete recived_bufferj;
		delete recived_buffer;
		delete obj.GetMatrixC();

		cout<<"break point 6"<<endl;
		obj.MatrixStore();
	}

	//end=clock();
	//	cout<<"Total Execution Time = "<<end-start<<endl;
	// ================== Simulation ================

	return 0;
}
